package com.makeunion.gdlbsdemo.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.makeunion.gdlbsdemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class MarkLineActivity extends AppCompatActivity {

    private MapView mapView;

    private AMap aMap;

    private Polyline polyline;

    public static final LatLng BEIJING = new LatLng(39.90403, 116.407525);// 北京
    public static final LatLng SHANGHAI = new LatLng(31.238068, 121.501654);// 上海
    public static final LatLng CHENGDU = new LatLng(30.679879, 104.064855);// 成都

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mark_line);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }
    }

    public void drawLine(View view) {
        if (polyline != null) {
            polyline.remove();
        }
        polyline = aMap.addPolyline((new PolylineOptions())
                .add(SHANGHAI, BEIJING, CHENGDU)
                .width(10)
                .setDottedLine(false)//设置虚线
                .color(Color.RED));
    }

    public void drawDashLine(View view) {
        if (polyline != null) {
            polyline.remove();
        }
        polyline = aMap.addPolyline((new PolylineOptions())
                .add(SHANGHAI, BEIJING, CHENGDU)
                .width(10)
                .setDottedLine(true)//设置虚线
                .color(Color.BLUE));
    }

    public void drawTextureLine(View view) {
        if (polyline != null) {
            polyline.remove();
        }
        //用一个数组来存放纹理
        List<BitmapDescriptor> texturesList = new ArrayList<>();
        texturesList.add(BitmapDescriptorFactory.fromResource(R.drawable.texture_1));
        texturesList.add(BitmapDescriptorFactory.fromResource(R.drawable.texture_2));

        //指定某一段用某个纹理，对应texturesList的index即可, 三个点对应两段颜色
        List<Integer> texIndexList = new ArrayList<>();
        texIndexList.add(0);//对应上面的第0个纹理
        texIndexList.add(1);//对应上面的第1个纹理

        PolylineOptions options = new PolylineOptions();
        options.width(30);//设置宽度

        //加入三个点
        options.add(SHANGHAI, BEIJING, CHENGDU);

        //加入对应的颜色,使用setCustomTextureList 即表示使用多纹理；
        options.setCustomTextureList(texturesList);

        //设置纹理对应的Index
        options.setCustomTextureIndex(texIndexList);
        polyline = aMap.addPolyline(options);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
